﻿using System.Linq;
using System.Data.Linq;
namespace UniMana.Data
{
    public static class Training_Student_SubjectData
    {
        #region [An]
        /// <summary>
        /// Lấy dữ liệu với StudentKey, SubjectKey, học kỳ, năm học
        /// </summary>
        /// <param name="nStudentSubject"></param>
        /// <param name="m_StudentKey"></param>
        /// <param name="m_SubjectKey"></param>
        /// <param name="m_Term"></param>
        /// <param name="m_SchoolYear"></param>
        /// <returns></returns>

        public static IQueryable GetByStudentSubject(this Table<Training_Student_Subject> nTraining_Student_Subject,
            Table<Training_Subject_Class> nTraining_Subject_Class,
            string m_strTraining_StudentKey,
            int m_iTraining_SubjectKey,
            int m_iCatalogue_TermKey,
            int m_iCatalogue_SchoolYearKey)
        {
            var record = from t in nTraining_Student_Subject
                         join t0 in nTraining_Subject_Class on t.Training_SubjectKey equals t0.Training_SubjectKey
                         where
                           t0.Opened == true &&
                           t.Training_SubjectKey == m_iTraining_SubjectKey &&
                           t.Training_StudentKey.ToString() == m_strTraining_StudentKey &&
                           t.Catalogue_TermKey == m_iCatalogue_TermKey &&
                           t.Catalogue_SchoolYearKey == m_iCatalogue_SchoolYearKey
                         select t;
            return record.Distinct();
        }
        #endregion

        #region Chi --Hòa
        /// <summary>
        /// Đếm số SV đã đăng ký môn này (Kiểm tra khi xóa môn mở)
        /// </summary>
        /// <param name="nStudentSubject"></param>
        /// <param name="_iSubjectOpenKey">Mã mở môn</param>
        /// <returns></returns>
        public static int GetStudentRegist(this Table<Training_Student_Subject> nStudentSubject, int _iSubjectOpenKey)
        {
            var iQuery = from query in nStudentSubject
                         where query.Training_SubjectOpenKey == _iSubjectOpenKey
                         select query.Training_Regist_SubjectKey;
            return iQuery.Count();
        }


        public static void DeleteStudentSubjectByRequestOpenKey(
            this Table<Training_Student_Subject> nTraining_Student_Subject,
            int _iTraining_SubjectOpenRequestKey)
        {
            DBMain DBMain = new DBMain();
            var iQuery = DBMain.m_DCSave.Training_Student_Subjects.Where(
                p => p.Training_SubjectOpenRequestKey == _iTraining_SubjectOpenRequestKey);
            DBMain.m_DCSave.Training_Student_Subjects.DeleteAllOnSubmit(iQuery);
            DBMain.m_DCSave.SubmitChanges();
        }
        #endregion
    }
}